#include "StdAfx.h"
#include "Link.h"

Link::Link(ImgResource ^res,int type,System::Windows::Forms::ContextMenuStrip ^menuRB,int link_from_point,int link_to_point
									,Action^ link_from,Action^ link_to,int fromN,int toN,int fromAN,int toAN)
{
	this->fromN = fromN;
	this->fromAN = fromAN;
	this->toN = toN;
	this->toAN = toAN;
	this->type = type;
	this->link_from_point = link_from_point;
	this->link_to_point = link_to_point;
	this->link_from = link_from;
	this->link_to = link_to;

	this->sr = gcnew System::Windows::Forms::PictureBox();			
	this->sr->Location = System::Drawing::Point(0, 0);
	this->sr->Size = System::Drawing::Size(0, 0);
	this->sr->TabStop = false;
	this->sr->ContextMenuStrip = menuRB;

	this->vert = gcnew System::Windows::Forms::PictureBox();		
	this->vert->Location = System::Drawing::Point(0, 0);
	this->vert->Size = System::Drawing::Size(0, 0);
	this->vert->TabStop = false;
	this->vert->ContextMenuStrip = menuRB;

	this->hort = gcnew System::Windows::Forms::PictureBox();		
	this->hort->Location = System::Drawing::Point(0, 0);
	this->hort->Size = System::Drawing::Size(0, 0);
	this->hort->TabStop = false;
	this->hort->ContextMenuStrip = menuRB;

	this->arrow = gcnew System::Windows::Forms::PictureBox();		
	this->arrow->Location = System::Drawing::Point(0, 0);
	this->arrow->Size = System::Drawing::Size(20, 20);
	this->arrow->TabStop = false;
	this->arrow->ContextMenuStrip = menuRB;
}

Link^ Link::MakeEqual(Link^ other,Action^ fromA,Action^ toA)
{
	this->fromN = other->fromN;
	this->fromAN = other->fromAN;
	this->toN = other->toN;
	this->toAN = other->toAN;
	this->type = other->type;
	if (this->type==-1)
	{
		this->null();
	}
	this->link_from_point = other->link_from_point;
	this->link_to_point = other->link_to_point;
	this->link_from = fromA;
	this->link_to = toA;
	return this;
}
System::Windows::Forms::PictureBox^  Link::getSr()
{
	return this->sr;
}
System::Windows::Forms::PictureBox^  Link::getVert()
{
	return this->vert;
}
System::Windows::Forms::PictureBox^  Link::getHort()
{
	return this->hort;
}
System::Windows::Forms::PictureBox^  Link::getArrow()
{
	return this->arrow;
}
int Link::get(char parameter)
{
	switch( parameter )
	{   
		case 't': 
		case 'T':  
			return this->type;
			break;
		case 'f': 
		case 'F':  
			return this->link_from_point;
			break;
		case 'p': 
		case 'P':  
			return this->link_to_point;
			break;   

		default:
			break;
	}
}
void Link::set(ImgResource ^res,int type,int link_from_point,int link_to_point,Action^ link_from,Action^ link_to,int fromN,int toN,int fromAN,int toAN)
{
	this->fromN = fromN;
	this->fromAN = fromAN;
	this->toN = toN;
	this->toAN = toAN;
	this->type = type;
	this->link_from_point = link_from_point;
	this->link_to_point = link_to_point;
	this->link_from = link_from;
	this->link_to = link_to;
	if ( type == 1 )
	{
		this->sr->BackgroundImage = res->link;
		this->vert->BackgroundImage = res->link;
		this->hort->BackgroundImage = res->link;
	}
	else
	{
		this->sr->BackgroundImage = res->link_c;
		this->vert->BackgroundImage = res->link_c;
		this->hort->BackgroundImage = res->link_c;
	}
}
void Link::paint(ImgResource ^res,int fromN,int toN,int fromAN,int toAN)
{
	 if (this->type > -1)
	 {
		 this->fromAN = fromAN;
		 this->fromN = fromN;
		 this->toAN = toAN;
		 this->toN = toN;
		 int changer = 0;
		 System::Drawing::Point a1,a2,a3,a4,arr;
		 System::Drawing::Size *s,*a,*a0;
		 //===============================================================================
		 if ((this->link_from->getActionImg()->Location.X - this->link_to->getActionImg()->Location.X)*(this->link_from->getActionImg()->Location.X - this->link_to->getActionImg()->Location.X) < 
			 (this->link_from->getActionImg()->Location.Y - this->link_to->getActionImg()->Location.Y)*(this->link_from->getActionImg()->Location.Y - this->link_to->getActionImg()->Location.Y))
		 {
			 this->link_from_point = 2;
			 this->link_to_point = 2;
		 }
		 else
		 {
			 this->link_from_point = 1;
			 this->link_to_point = 1;
		 }
		 //===============================================================================
		 if (this->link_from->get('t') != 6)
		 {
			 a1.X = this->link_from->getActionImg()->Location.X + this->link_from->getActionImg()->Size.Width/2;
		 }
		 else
		 {
			 a1.X = this->link_from->getActionImg()->Location.X + ((this->fromN*this->link_from->getActionImg()->Size.Width)/this->fromAN);
			 this->link_from_point = 1;
		 }
		 if (this->link_from->get('t') != 5)
		 {
			 a1.Y = this->link_from->getActionImg()->Location.Y + this->link_from->getActionImg()->Size.Height/2;
		 }
		 else
		 {
			 a1.Y = this->link_from->getActionImg()->Location.Y + ((this->fromN*this->link_from->getActionImg()->Size.Height)/this->fromAN);
			 this->link_from_point = 2;
		 }
		 if (this->link_to->get('t') != 6)
		 {
			 a3.X = this->link_to->getActionImg()->Location.X + this->link_to->getActionImg()->Size.Width/2;
		 }
		 else
		 {
			 a3.X = this->link_to->getActionImg()->Location.X + ((toN*this->link_to->getActionImg()->Size.Width)/toAN);
			 this->link_to_point = 1;
		 }
		 if (this->link_to->get('t') != 5)
		 {
			 a3.Y = this->link_to->getActionImg()->Location.Y + this->link_to->getActionImg()->Size.Height/2;
		 }
		 else
		 {
			 a3.Y = this->link_to->getActionImg()->Location.Y + ((toN*this->link_to->getActionImg()->Size.Height)/toAN);
			 this->link_to_point = 2;
		 }
		 a0=new System::Drawing::Size(0,0);
		 if (((this->link_from_point == 1) || (this->link_from_point == 4)) && ((this->link_to_point == 1) || (this->link_to_point == 4)))
		 {
			 if (a1.Y > a3.Y)
			 {
				 changer = 1;
				 a2 = a1;
				 a1 = a3; //pointed
				 a3 = a2;
				 arrow->BackgroundImage = res->arrowImg_up;
				 arr.X = a1.X - 8;
				 arr.Y = this->link_to->getActionImg()->Location.Y + this->link_to->getActionImg()->Size.Height;
			 }
			 else
			 {
				 arrow->BackgroundImage = res->arrowImg_down;
				 arr.X = a3.X - 8;
				 arr.Y = this->link_to->getActionImg()->Location.Y - 20;
			 }
			 if (a1.X < a3.X)
			 {
				 a2.X = a1.X;
				 s=new System::Drawing::Size(a3.X - a1.X + 4,4);
			 }
			 else
			 {
				 a2.X = a3.X;
				 s=new System::Drawing::Size(a1.X - a3.X + 4,4);
			 }
			 a4.X = a3.X;
			 a4.Y = a3.Y - (a3.Y - a1.Y)/2;
			 a2.Y = a4.Y;
			 vert->Location = a1;
			 hort->Location = a4;
			 sr->Location = a2;
			 if ((a2.Y >= (this->link_to->getActionImg()->Location.Y - 20)) && (a2.Y <= (this->link_to->getActionImg()->Location.Y + this->link_to->getActionImg()->Size.Height + 20)))
			 {
				 arr.Y = a2.Y - 8;
				 if (((changer == 0) && !(a1.X < a3.X)) || ((changer == 1) && (a1.X < a3.X)))
				 {
					 arrow->BackgroundImage = res->arrowImg_left;
					 arr.X = this->link_to->getActionImg()->Location.X + this->link_to->getActionImg()->Size.Width;
				 }
				 else
				 {
					 arrow->BackgroundImage = res->arrowImg_right;
					 arr.X = this->link_to->getActionImg()->Location.X - 20;
				 }
			 }
			 a=new System::Drawing::Size(4,(a3.Y - a1.Y)/2 + 4);
			 vert->Size = *(a);
			 hort->Size = *(a);
			 sr->Size = *(s);
			 delete a;
			 delete s;
		 }
		 if (((this->link_from_point == 2) || (this->link_from_point == 3)) && ((this->link_to_point == 2) || (this->link_to_point == 3)))
		 {
			 if (a1.X > a3.X)
			 {
				 changer = 1;
				 a2 = a1;
				 a1 = a3;
				 a3 = a2;
				 arrow->BackgroundImage = res->arrowImg_left;
				 arr.Y = a1.Y - 8;
				 arr.X = this->link_to->getActionImg()->Location.X + this->link_to->getActionImg()->Size.Width;
			 }
			 else
			 {
				 arrow->BackgroundImage = res->arrowImg_right;
				 arr.Y = a3.Y - 8;
				 arr.X = this->link_to->getActionImg()->Location.X - 20;
			 }
			 if (a1.Y < a3.Y)
			 {
				 a2.Y = a1.Y;
				 s=new System::Drawing::Size(4,a3.Y - a1.Y + 4);
			 }
			 else
			 {
				 a2.Y = a3.Y;
				 s=new System::Drawing::Size(4,a1.Y - a3.Y + 4);
			 }
			 a4.Y = a3.Y;
			 a4.X = a3.X - (a3.X - a1.X)/2;
			 a2.X = a4.X;
			 vert->Location = a1;
			 hort->Location = a4;
			 sr->Location = a2;
			 if ((a2.X >= (this->link_to->getActionImg()->Location.X - 20)) && (a2.X <= (this->link_to->getActionImg()->Location.X + this->link_to->getActionImg()->Size.Width + 20)))
			 {
				 arr.X = a2.X - 8;
				 if (((changer == 0) && !(a1.Y < a3.Y)) || ((changer == 1) && (a1.Y < a3.Y)))
				 {
					 arrow->BackgroundImage = res->arrowImg_up;
					 arr.Y = this->link_to->getActionImg()->Location.Y + this->link_to->getActionImg()->Size.Height;
				 }
				 else
				 {
					 arrow->BackgroundImage = res->arrowImg_down;
					 arr.Y = this->link_to->getActionImg()->Location.Y - 20;
				 }
			 }
			 a=new System::Drawing::Size((a3.X - a1.X)/2 + 4,4);
			 vert->Size = *(a);
			 hort->Size = *(a);
			 sr->Size = *(s);
			 delete a;
			 delete s;
		 }
		 if (((this->link_from_point == 1) || (this->link_from_point == 4)) && ((this->link_to_point == 2) || (this->link_to_point == 3)))
		 {
			 a4.X = a1.X;
			 if (a1.Y > a3.Y)
			 {
				 a4.Y = a3.Y;
				 a=new System::Drawing::Size(4,a1.Y - a3.Y + 4);
			 }
			 else
			 {
				 a4.Y = a1.Y;
				 a=new System::Drawing::Size(4,a3.Y - a1.Y + 4);
			 }
			 if (a1.X < a3.X)
			 {
				 a2.X = a1.X;
				 s=new System::Drawing::Size(a3.X - a1.X + 4,4);
				 arrow->BackgroundImage = res->arrowImg_right;
				 arr.Y = a3.Y - 8;
				 arr.X = this->link_to->getActionImg()->Location.X - 20;
			 }
			 else
			 {
				 a2.X = a3.X;
				 s=new System::Drawing::Size(a1.X - a3.X + 4,4);
				 arrow->BackgroundImage = res->arrowImg_left;
				 arr.Y = a3.Y - 8;
				 arr.X = this->link_to->getActionImg()->Location.X + this->link_to->getActionImg()->Size.Width;
			 }
			 a2.Y = a3.Y;
			 vert->Location = a4;
			 hort->Location = a2;
			 vert->Size = *(a);
			 hort->Size = *(s);
			 sr->Size = *(a0);
			 if ((a1.X >= (this->link_to->getActionImg()->Location.X - 20)) && (a1.X <= (this->link_to->getActionImg()->Location.X + this->link_to->getActionImg()->Size.Width + 20)))
			 {
				 arr.X = a1.X - 8;
				 if ((a1.Y < a3.Y) || (a1.Y < a3.Y))
				 {
					 arrow->BackgroundImage = res->arrowImg_down;
					 arr.Y = this->link_to->getActionImg()->Location.Y - 20;
				 }
				 else
				 {
					 arrow->BackgroundImage = res->arrowImg_up;
					 arr.Y = this->link_to->getActionImg()->Location.Y + this->link_to->getActionImg()->Size.Height;
				 }
			 }
			 delete a;
			 delete s;
		 }
		 if (((this->link_from_point == 2) || (this->link_from_point == 3)) && ((this->link_to_point == 1) || (this->link_to_point == 4)))
		 {
			 a4.Y = a1.Y;
			 if (a1.X > a3.X)
			 {
				 a4.X = a3.X;
				 a=new System::Drawing::Size(a1.X - a3.X + 4,4);
			 }
			 else
			 {
				 a4.X = a1.X;
				 a=new System::Drawing::Size(a3.X - a1.X + 4,4);
			 }
			 if (a1.Y < a3.Y)
			 {
				 a2.Y = a1.Y;
				 s=new System::Drawing::Size(4,a3.Y - a1.Y + 4);
				 arrow->BackgroundImage = res->arrowImg_down;
				 arr.X = a3.X - 8;
				 arr.Y = this->link_to->getActionImg()->Location.Y - 20;
			 }
			 else
			 {
				 a2.Y = a3.Y;
				 s=new System::Drawing::Size(4,a1.Y - a3.Y + 4);
				 arrow->BackgroundImage = res->arrowImg_up;
				 arr.X = a3.X - 8;
				 arr.Y = this->link_to->getActionImg()->Location.Y + this->link_to->getActionImg()->Size.Height;
			 }
			 a2.X = a3.X;
			 vert->Location = a4;
			 hort->Location = a2;
			 vert->Size = *(a);
			 hort->Size = *(s);
			 sr->Size = *(a0);
			 if ((a1.Y >= (this->link_to->getActionImg()->Location.Y - 20)) && (a1.Y <= (this->link_to->getActionImg()->Location.Y + this->link_to->getActionImg()->Size.Height + 20)))
			 {
				 arr.Y = a1.Y - 8;
				 if ((a1.X < a3.X) || (a1.X < a3.X))
				 {
					 arrow->BackgroundImage = res->arrowImg_right;
					 arr.X = this->link_to->getActionImg()->Location.X - 20;
				 }
				 else
				 {
					 arrow->BackgroundImage = res->arrowImg_left;
					 arr.X = this->link_to->getActionImg()->Location.X + this->link_to->getActionImg()->Size.Width;
				 }
			 }
			 delete a;
			 delete s;
		 }
		 vert->SendToBack();
		 hort->SendToBack();
		 sr->SendToBack();

		 arrow->Location = arr;
		 arrow->Size = System::Drawing::Size(20, 20);
		 arrow->Visible = true;
		 arrow->SendToBack();
	 }
}
void Link::null()
{
	this->type = -1;
	this->link_from_point = 0;
	this->link_to_point = 0;
		
	this->sr->Location = System::Drawing::Point(0, 0);
	this->sr->Size = System::Drawing::Size(0, 0);
		
	this->vert->Location = System::Drawing::Point(0, 0);
	this->vert->Size = System::Drawing::Size(0, 0);
		
	this->hort->Location = System::Drawing::Point(0, 0);
	this->hort->Size = System::Drawing::Size(0, 0);
		
	this->arrow->Location = System::Drawing::Point(0, 0);
	this->arrow->Size = System::Drawing::Size(0, 0);
}